package com.hzhjhh.view.manageView.menus.accountMng.register;

import java.util.Objects;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import org.apache.ibatis.session.SqlSession;

import com.hzhjhh.constant.DateFormatContants;
import com.hzhjhh.entity.UserInfo;
import com.hzhjhh.mapper.UserInfoMapper;
import com.hzhjhh.util.DBUtils;
import com.hzhjhh.view.manageView.menus.accountMng.RegisterDialog;

import cn.hutool.core.date.DateUtil;

/**
 * 菜单栏初始化-仓库-账户管理-添加/修改用户-管理员登录弹出框-修改用户弹出框-修改
 * 
 * @author 12083
 *
 */
public class UpdateBt {

	private static UserInfoMapper userInfoMapper;
	
	/**
	 * 按钮
	 */
	public static JButton btInit(RegisterDialog registerDialog) {
		JButton update = new JButton();
		update.setBounds(175, 318, 83, 25);
		update.setText("修改");

		update.addActionListener(event -> {
        	String cardId = registerDialog.getTxfCardId().getText(); //卡号
        	UserInfo userInfo = userInfoMapper.selectByCardId(cardId);
//            Vector Select_vector = new Vector();
//            Select_vector = Select_Means.Select_UserManager(cardId); //查询此卡是否存在
            if (Objects.isNull(userInfo)) {
                JOptionPane.showMessageDialog(registerDialog, "卡号不存在", "提示",
                        JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            userInfo.setUserName(registerDialog.getTxfCardOwner().getText());
            userInfo.setUserPassword(registerDialog.getPasswordField().getText());
            userInfo.setUserType(registerDialog.getCobUserType().getSelectedItem().toString());
            userInfo.setCardId(Integer.parseInt(cardId));
            userInfo.setDiscount(Integer.parseInt(registerDialog.getTxfDiscount().getText()));
            userInfo.setUserId(registerDialog.getTxfIDCard().getText());
            userInfo.setCardDate(DateUtil.parse(registerDialog.getTxfCardOpenDate().getText(), DateFormatContants.YYYY_MM_DD_HH_MM_SS));
            userInfo.setTerm(registerDialog.getTxfTerm().getText());
            userInfo.setScore(Integer.parseInt(registerDialog.getTxfScore().getText()));
            userInfo.setRemark(registerDialog.getTxaRemark().getText());
            int row = userInfoMapper.update(userInfo);
            
//            int i = UpdateData.UpdateCard(cardId, 
//            		registerDialog.getTxfCardOwner().getText(),
//                    registerDialog.getTxfIDCard().getText(),
//                    registerDialog.getPasswordField().getText(),
//                    registerDialog.getTxfCardOpenDate().getText(),
//                    registerDialog.getCobUserType().getSelectedItem().toString(),
//                    registerDialog.getTxfTerm().getText(),
//                    registerDialog.getTxfScore().getText(),
//                    registerDialog.getTxfDiscount().getText(), 
//                    registerDialog.getTxaRemark().getText()
//            );
            if (row == 0) {
                JOptionPane.showMessageDialog(registerDialog, "资料更新失败", "提示",
                        JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(registerDialog, "资料更新成功", "提示",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        });
		
		// 初始化mapper
		SqlSession sqlSession = DBUtils.getSqlSession(true);
		userInfoMapper = sqlSession.getMapper(UserInfoMapper.class);
		
		return update;
	}
}
